package client;

import commun.BanqueService;
import commun.Compte;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class ClientService {    
    private final String host = "127.0.0.1";
    private String nom;
    private String password;
    private int somme;
    private Compte c;
    
    
    public ClientService(){}

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getSomme() {
        return somme;
    }

    public void setSomme(int somme) {
        this.somme = somme;
    }

    public Compte getC() {
        return c;
    }

    public void setC(Compte c) {
        this.c = c;
    }

    public void ouvrirCompte(String id, String password) {
        try {
            BanqueService srv = (BanqueService) Naming.lookup("rmi://" + host + "/ServeurRMI");
            srv.ouvrirCompte(id, password);
        } catch (NotBoundException | MalformedURLException | RemoteException e) {
            System.out.println(e.toString());
        }
    }

    public Compte verifie(String id, String password) {
        Compte compte = null;
        try {
            BanqueService srv = (BanqueService) Naming.lookup("rmi://" + host + "/ServeurRMI");
            Compte compte1 = srv.verifie(id, password);
            if (compte1 != null) {
                compte = compte1;
            }
        } catch (NotBoundException | MalformedURLException | RemoteException e) {
            System.out.println(e.toString());
        }
        return compte;
    }

    public int fermerCompte(String id, String password) {
        int i = 0;
        try {
            BanqueService srv = (BanqueService) Naming.lookup("rmi://" + host + "/ServeurRMI");
            i = srv.fermerCompte(id, password);
        } catch (NotBoundException | MalformedURLException | RemoteException e) {
            System.out.println(e.toString());
        }
        return i;
    }

    public void deposer(int somme, Compte c) {
        try {
            BanqueService srv = (BanqueService) Naming.lookup("rmi://" + host + "/ServeurRMI");
            srv.deposer(somme, c);
        } catch (NotBoundException | MalformedURLException | RemoteException e) {
            System.out.println(e.toString());
        }
    }

    public int retirer(int somme, Compte c) {
        int i = 0;
        try {
            BanqueService srv = (BanqueService) Naming.lookup("rmi://" + host + "/ServeurRMI");
            i = srv.retirer(somme, c);
        } catch (NotBoundException | MalformedURLException | RemoteException e) {
            System.out.println(e.toString());
        }
        return i;
    }

    public int voirSolde(Compte c) {
        int i = 0;
        try {
            BanqueService srv = (BanqueService) Naming.lookup("rmi://" + host + "/ServeurRMI");
            i = srv.voirSolde(c);
        } catch (NotBoundException | MalformedURLException | RemoteException e) {
            System.out.println(e.toString());
        }
        return i;
    }

    public boolean connecter(String id) {
        boolean b = false; // true = connecter et false = compte en cours d'utilisation
        try {
            BanqueService srv = (BanqueService) Naming.lookup("rmi://" + host + "/ServeurRMI");
            b = srv.connecter(id);
        } catch (NotBoundException | MalformedURLException | RemoteException e) {
            System.out.println(e.toString());
        }
        return b;
    }

    public void deconnecter(String id) {
        try {
            BanqueService srv = (BanqueService) Naming.lookup("rmi://" + host + "/ServeurRMI");
            srv.deconnecter(id);
        } catch (NotBoundException | MalformedURLException | RemoteException e) {
            System.out.println(e.toString());
        }
    }
}
